from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Repository(Base):
    __tablename__ = 'repositories'
    
    id  = Column(Integer, primary_key=True)
    url = Column(String(255), nullable=False, unique=True)

class Revision(Base):
    __tablename__ = 'revisions'
    
    id       = Column(Integer, primary_key=True)
    author   = Column(Unicode(32), nullable=False)
    message  = Column(UnicodeText, nullable=True)
    date     = Column(DateTime, nullable=False)
    rev_num  = Column(Integer, nullable=False)
    
    repository_id = Column(Integer, ForeignKey('repositories.id'))
    repository    = relation(Repository, backref=backref('repository'))
    
class Change(Base):
    __tablename__ = 'changes'
    
    id      = Column(Integer, primary_key=True)
    action  = Column(Unicode(4))
    path    = Column(Unicode(512))
    
    revision_id  = Column(Integer, ForeignKey('revisions.id'))
    revision     = relation(Revision, backref=backref('revision'))

repositories_table = Repository.__table__
revisions_table = Revision.__table__
changes_table = Change.__table__

revisions_table.append_constraint(UniqueConstraint('repository_id', 'rev_num'))
